package com.safeluck.scannerqcr.util;

import com.orhanobut.logger.Logger;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * ScannerQCR
 * Created by lzw on 2017/11/29. 17:13:11
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */

public class HttpConnectUtil {
    public static String postStage(String json, String urls) throws Exception {

        StringBuilder sb = new StringBuilder();
        sb.append(urls).append("");

        URL url = new URL(sb.toString());
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
        connection.connect();
        DataOutputStream out = new DataOutputStream(
                connection.getOutputStream());
        JSONObject obj = new JSONObject(json);
        Logger.d("参数请求:"+obj.toString());

        out.writeBytes(obj.toString());
        out.flush();
        out.close();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String lines;
        StringBuffer result = new StringBuffer("");
        while ((lines = reader.readLine()) != null) {
            lines = new String(lines.getBytes(), "utf-8");
            result.append(lines);
        }
        reader.close();
        connection.disconnect();
        return result.toString();

    }

}
